package com.last1mile.dao.ibatis.impl;

import java.sql.SQLException;
import java.util.List;

import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.last1mile.dao.BaseSqlMapClientDao;
import com.last1mile.dao.interfaces.ServeDestDao;
import com.last1mile.pojo.ServeDestDO;

@Component("serveDestDao")
public class ServeDestDaoImpl extends BaseSqlMapClientDao implements ServeDestDao {

    @Override
    public Integer insertServeDest(ServeDestDO serveDestDo) {
        return (Integer) getSqlMapClientTemplate().insert("serveDest.insertServeDest", serveDestDo);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ServeDestDO> queryServeDestByServeId(Integer serveId) {
        ServeDestDO key = new ServeDestDO();
        key.setServeId(serveId);
        List<ServeDestDO> records = (List<ServeDestDO>) getSqlMapClientTemplate().queryForList(
                                                                                               "serveDest.selectByServeId",
                                                                                               key);
        return records;
    }

    @SuppressWarnings("unchecked")
    @Override
    public void insertServeDestList(final List<ServeDestDO> destList) {
        if(!CollectionUtils.isEmpty(destList)){
            this.getSqlMapClientTemplate().execute(new SqlMapClientCallback(){
                public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { 
                    executor.startBatch(); 
                    for (int i = 0, n = destList.size(); i < n; i++) { 
                        executor.insert("serveDest.insertServeDest", destList.get(i)); 
                    } 
                    executor.executeBatch(); 
                    return null; 
                 }
            });
        }
    }

}
